﻿Imports System.IO

Namespace Editor.NPC
    Module mNPCs

        'npc definition/data struct
        Friend Structure NPCDef
            Dim ID As Integer 'npc identifier
            'coordinates on map
            Dim X As Integer
            Dim Y As Integer
            Dim Shape As Integer 'shape determines which npc to draw
            Dim Name As String 'name to display
            Dim CommandStr As String 'script command string
            'animation vars
            Dim AnimTick As Long
            Dim AnimIndex As Integer
            Dim AnimCount As Integer
        End Structure

        Friend NPCList As List(Of NPCDef) 'list storing npc definitions/animation data

        Friend SelectedNPCID As Integer = 4 '-1 'selected npc id, -1 = none selected

        Friend Const NPC_AnimSpeed As Long = 5000000 'animation speed constant

        Friend Sub LoadMapNPCs()

            'initialize npc list
            NPCList = New List(Of NPCDef)

            'store path to current maps npc def file
            Dim NPCDefFile As String = Main.sServerPath & "\NPCDef\" & Editor.mCurrentMapFilename & ".txt"

            'check if def file doesn't exist
            If Not File.Exists(NPCDefFile) Then

                'create file and add header
                Dim WStream As TextWriter = File.CreateText(NPCDefFile)
                WStream.WriteLine(";ID	X	Y	SHAPE	NAME	CMDSTR")
                WStream.Flush()
                WStream.Close()

            End If

            'open def file for write access
            Dim Stream As TextReader = File.OpenText(NPCDefFile)
            Dim Line As String = "" 'stores line contents
            Dim LineNo As Integer 'store line number

            Do While Stream.Peek >= 0

                Line = Stream.ReadLine()

                'increment line number by 1
                LineNo += 1

                'check for comment line, if so, ignore
                If Strings.Left(Line, 1) <> ";" Then

                    'trim tabs
                    TrimLineTabs(Line)

                    Dim tmpDef As NPCDef 'temporary var to hold npc definition

                    Try

                        'fill npc def struct
                        tmpDef.ID = ReadNextValue(Line)
                        tmpDef.X = ReadNextValue(Line)
                        tmpDef.Y = ReadNextValue(Line)
                        tmpDef.Shape = ReadNextValue(Line)
                        tmpDef.Name = ReadNextValue(Line)
                        tmpDef.CommandStr = ReadNextValue(Line)

                        tmpDef.AnimCount = GetNPCAnimCount(GetNPCImageIndex(tmpDef.Shape))
                        tmpDef.AnimIndex = 0
                        tmpDef.AnimTick = DateTime.Now.Ticks

                        'add to the list
                        NPCList.Add(tmpDef)

                    Catch
                        'error reading the line, display error message
                        MsgBox("Editor.NPCDef.LoadMapNPCs(): Error on line #" & LineNo & " of " & NPCDefFile & " (probably missing values)", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly, "Server config error")
                    End Try

                End If

            Loop

            Stream.Close()

        End Sub

        Public Sub DrawNPC(X As Integer, Y As Integer, pX As Double, pY As Double, z As Single, Optional HighlightedOnly As Boolean = False)

            'get current tick count
            Dim TickCount As Long = DateTime.Now.Ticks

            Dim npcD As NPC.NPCDef

            'iterate through NPC's
            For iT As Integer = 0 To NPCList.Count - 1

                npcD = NPCList(iT)

                'check if NPC is at these coordinates
                If npcD.X = X And npcD.Y = Y Then

                    'animation timer
                    If npcD.AnimTick < TickCount Then
                        npcD.AnimTick = TickCount + NPC_AnimSpeed
                        npcD.AnimIndex += 1
                        If npcD.AnimIndex = npcD.AnimCount Then
                            npcD.AnimIndex = 0
                        End If
                    End If

                    'use current image
                    With Libs.NPC.Image(GetNPCImageIndex(npcD.Shape) + npcD.AnimIndex)

                        '/////////SELECTED NPCS DIFF

                        'check if we are drawing the selected NPC and if this is the selected one
                        'If (SelectedNPCID = npcD.ID) Then
                        '    If HighlightedOnly Then
                        '        'draw highlighted npc
                        '        DirectX.BlitD3D(.Texture, pX + .Header.OffsetX, pY + .Header.OffsetY, .Header.Width, .Header.Height, z, Editor.arrZoom(Editor.ZoomIndex))
                        '        'check if we should be drawing the npc symbol
                        '        If Editor.bShowSymbols Then
                        '            'draw highlighted npc symbol
                        '            DirectX.BlitD3D(Libs.Editor.Image(2).Texture, ((pX + (Editor.TileWidth / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, ((pY + (Editor.TileHeight / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, Libs.Editor.Image(2).Header.Width, Libs.Editor.Image(2).Header.Height, 0.02F, 1.0F)
                        '        End If
                        '    End If
                        'ElseIf Not HighlightedOnly Then
                        '    'draw npc normally (not selected)
                        '    DirectX.BlitD3D(.Texture, pX + .Header.OffsetX, pY + .Header.OffsetY, .Header.Width, .Header.Height, z, Editor.arrZoom(Editor.ZoomIndex))
                        '    'check if we should be drawing the npc symbol
                        '    If Editor.bShowSymbols Then
                        '        'draw npc symbol
                        '        DirectX.BlitD3D(Libs.Editor.Image(2).Texture, ((pX + (Editor.TileWidth / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, ((pY + (Editor.TileHeight / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, Libs.Editor.Image(2).Header.Width, Libs.Editor.Image(2).Header.Height, 0.02F, 1.0F)
                        '    End If
                        'End If

                        '///////SELECTED NPCS NORMAL
                        If Not HighlightedOnly Then
                            'draw npc normally (not selected)
                            DirectX.BlitD3D(.Texture, pX + .Header.OffsetX, pY + .Header.OffsetY, .Header.Width, .Header.Height, z, Editor.arrZoom(Editor.ZoomIndex))
                            'check if we should be drawing the npc symbol
                            If Editor.bShowSymbols Then
                                'draw npc symbol
                                DirectX.BlitD3D(Libs.Editor.Image(cWilIdx_NPC).Texture, ((pX + (Editor.TileWidth / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, ((pY + (Editor.TileHeight / 2)) * Editor.arrZoom(Editor.ZoomIndex)) - 32, Libs.Editor.Image(2).Header.Width, Libs.Editor.Image(2).Header.Height, 0.02F, 1.0F)
                            End If
                        End If

                    End With

                    NPCList(iT) = npcD

                    Exit For

                End If

            Next

        End Sub

        'return npc id from coordinates, -1 = no npc there
        Public Function GetNPCID(X As Integer, Y As Integer) As Integer

            For Each npcD As NPCDef In NPCList
                If npcD.X = X And npcD.Y = Y Then
                    Return npcD.ID
                End If
            Next

            Return -1

        End Function

        'returns start index for NPC given the shape
        Private Function GetNPCImageIndex(Shape As Integer) As Integer
            Return ((Shape * 100) + 1)
        End Function

        'calculates images in an npc's animation given the shape
        Private Function GetNPCAnimCount(start As Integer) As Integer

            Dim C As Integer

            Do Until Libs.NPC.Image(start + C).HasImage = False
                C += 1
            Loop

            Return C

        End Function

    End Module
End Namespace
